package com.smiier.skin;

import java.util.HashMap;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import cn.o.app.json.JsonUtil;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

/**
 * @{# UpdateInfoInputActivity.java Create on 2014-6-16 下午11:18:09
 * 
 * @author <a href="mailto:evan0502@qq.com">Evan</a>
 * @version 1.0 用作修改一些简单信息的通用界面
 */

public class UpdateInfoInputActivity extends BasicActivity {

	private String param_key, title_value;

	private EditText edit_common;

	// 标识是否需要返回，true为不保存，并返回，false为上传数据
	private boolean isReturn = true;
	private boolean isBingli = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_info_input_common);

		init();

		isReturn = getIntent().getBooleanExtra(Constant.IDENTITY_KEY, false);
		isBingli = getIntent().getBooleanExtra(Constant.IDENTITY_KEY_BINGLI,
				false);

		param_key = getIntent().getStringExtra(Constant.IDENTITY_KEY_PARAM_KEY);
		title_value = getIntent().getStringExtra(
				Constant.IDENTITY_KEY_NAV_TITLE);

		setNavTitle(title_value);
		setNavRightBtn("保存");

		edit_common = (EditText) findViewById(R.id.edit_common);
		int inputType = getIntent().getIntExtra(Constant.IDENTITY_KEY_1, -1);
		if (inputType != -1) {
			edit_common.setInputType(inputType);

			final int[] scope = getIntent().getIntArrayExtra(
					Constant.IDENTITY_KEY_2);
			if (!CommonUtility.isNull(scope) && scope.length > 0) {
				edit_common.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence txt, int start,
							int before, int count) {
						if (CommonUtility.isNull(txt)) {
							txt = "0";
						}
						float cost = Float.parseFloat(txt.toString());
						if (cost < scope[0] || cost > scope[1]) {
							CommonUtility.tip(activity, "请输入" + scope[0] + "-"
									+ scope[1] + "之间的数字");
							edit_common.setText(null);
							return;
						}
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {

					}

					@Override
					public void afterTextChanged(Editable s) {

					}
				});
			}
		}

		CommonUtility.showKeyboard(edit_common);
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			final String value = edit_common.getText().toString();
			if (param_key != null && param_key.equals("Nick")) {
				if (value.length() < 6) {
					CommonUtility.tip(activity, "6-18位字母、数据、下划线");
					return;
				}
			}
			if (!CommonUtility.isNull(value)) {
				int[] scope = getIntent().getIntArrayExtra(
						Constant.IDENTITY_KEY_2);
				if (!CommonUtility.isNull(scope) && scope.length > 0) {
					if (Long.parseLong(value) < 0
							|| Long.parseLong(value) > 10000) {
						CommonUtility.tip(activity, "请输入0-10000之间的数字");
						return;
					}
				}
				if (!isReturn) {
					HashMap<String, Object> params = new HashMap<String, Object>();
					params.put(param_key, value);
					if (isBingli) {
						params.put(Constant.PARAM_API, "MR.User.Set");
						if (!CommonUtility.isNull(GlobalSettings.sBingLi)) {
							try {
								params.put("MRUID", GlobalSettings.sBingLi.Uid);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					} else {
						params.put(Constant.PARAM_API, "User.Set");
					}
					params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
					RequestTaskIntercept.requestIntercept(activity,
							Constant.URL.REQUEST, new RequestHandler(activity,
									new HandlerCallback() {

										@Override
										public void callback(Object o) {
											if (!CommonUtility.isNull(o)) {
												JSONObject object = (JSONObject) o;
												try {
													int resultCode = object
															.getInt(Constant.JSON_PARAM_RESCODE);
													if (CommonUtility
															.response200(object)) {
														CommonUtility.toast(
																activity,
																"保存成功");
														try {
															if (isBingli) {
																GlobalSettings.sBingLi = JsonUtil
																		.convertFromObject(
																				object.getJSONObject("Res"),
																				User.class);
															} else {
																GlobalSettings.sUser = JsonUtil
																		.convertFromObject(
																				object.getJSONObject("Res"),
																				User.class);
															}
															finish();
														} catch (Exception e) {
															e.printStackTrace();
														}
													} else if (resultCode == -2005) {
														CommonUtility
																.tip(activity,
																		"您已经认证或者正在等待审核认证中，不可修改数据。");
													} else {
														CommonUtility.tip(
																activity,
																"修改失败，请稍后再试。");
													}
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										}
									}), params);
				} else {
					Intent data = new Intent();
					data.putExtra(Constant.IDENTITY_KEY, value);
					setResult(0, data);
					finish();
				}
			} else {
				CommonUtility.tip(activity, "请输入内容");
			}
		} else if (id == R.id.btn_clear) {
			edit_common.setText(null);
		}
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
